package cn.com.pc.content.domain.tag;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.*;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 标签关系
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
@Entity
@Table(name = "tag_relations")
public class TagRelation {
    @EmbeddedId
    private TagRelationKey relationKey;

    @Column(name = "relation")
    private String relation;

    @JsonFormat(timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    @ToString
    @Builder
    @Embeddable
    public static class TagRelationKey implements Serializable {
        @ManyToOne
        @JoinColumn(name = "left_id")
        private Tag left;

        @ManyToOne
        @JoinColumn(name = "right_id")
        private Tag right;
    }
}
